import 'package:flutter/material.dart';
import 'package:jiaju_flutter/routes.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  var _off = true;
  _buildPasswordWidget(BuildContext context) {
    var icons = _off ? Icons.visibility_off : Icons.visibility;
    return Flex(
      direction: Axis.horizontal,
      children: [
        Expanded(
          flex: 1,
          child: TextField(
            decoration: InputDecoration(
              labelText: "密码：",
              hintText: "6-8位密码",
              suffixIcon: InkWell(
                child: Icon(icons),
                onTap: () {
                  setState(() {
                    _off = !_off;
                  });
                },
              ),
            ),
            obscureText: _off,
            maxLength: 8,
            keyboardType: TextInputType.visiblePassword,
          ),
        ),
      ],
    );
  }

  _buildInputWidget(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 40.0, left: 20.0, right: 20.0),
      child: Column(
        children: [
          TextField(
            maxLength: 11,
            decoration: InputDecoration(
              labelText: "用户名：",
              hintText: "手机号",
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 10.0),
            child: _buildPasswordWidget(context),
          ),
        ],
      ),
    );
  }

  _buildLoginBtnWidget(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return Container(
      width: width,
      margin: EdgeInsets.only(top: 10),
      alignment: Alignment.center,
      child: ElevatedButton(
        style: ButtonStyle(
          minimumSize: MaterialStateProperty.all(Size(width - 40, 35)),
          backgroundColor: MaterialStateProperty.all(Colors.green),
        ),
        onPressed: () => {},
        child: Text("登录"),
      ),
    );
  }

  _buildRegisterWidget(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return Container(
      width: width,
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.only(right: 5.0),
            child: Text("还没有账号，"),
          ),
          TextButton(
              onPressed: () =>
                  Navigator.pushReplacementNamed(context, Routes.registor),
              child: Text(
                "去注册~",
                style: TextStyle(color: Colors.green),
              ))
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var width = size.width;
    var height = size.height;
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: Container(
        width: width,
        height: height,
        child: Column(
          children: [
            _buildInputWidget(context),
            _buildLoginBtnWidget(context),
            _buildRegisterWidget(context),
          ],
        ),
      ),
    );
  }
}
